import {
	isBlank
} from './stringNull.js'
import Vue from 'vue';


class DataManager {
	constructor() {}

	//处理数据
	useCodeData(res) {
		let data = res;
		let buildArray = [];
		if (!!data && data.length > 0) {
			for (let i = 0; i < data.length; i++) {
				let item = data[i];
                let properties = this.getProperties(item);
                // 增加默认等级 add by ray on 2021/01/15
                properties.LEVEL = -1;
				let buildGeoJson = {
					"type": "Feature",
					"geometry": item.areainfo,
					"properties": properties,
					"id": [i]
				};
				buildArray.push(buildGeoJson)
			}
			return buildArray;
		} else {
			return buildArray;
		}

	}

	getProperties(item) {
		let properties = {
			LON: item.lon || '',
			LAT: item.lat || '',
			SHENGBM: item.shengdm || "",
			SHENGMC: item.shengmc || "",
			SBM: item.shidm || "",
			SMC: item.shimc || "",
			QBM: item.qudm || "",
			QMC: item.qumc || "",
			JDBM: item.jddm || '',
			JDMC: item.jdmc || '',
			SQBM: item.sqdm || '',
			SQMC: item.sqmc || '',
			JCWGBM: item.wgdm || '',
			JCWGMC: item.wgmc || '',
			JZWTYDZBM: item.lddm || '',
			LDMC: item.ldmc || ''
		};
		//这里字段不全
		let address = properties.SHENGMC + properties.SMC + properties.QMC + properties.JDMC
		let address1;
		let zoom;
		if (item.areatype == '5') {
			//楼栋
			address1 = address + properties.SQMC + properties.JCWGMC + properties.LDMC
			zoom = 18;
		} else if (item.areatype == '4') {
			//网格
			address1 = address + properties.SQMC + properties.JCWGMC
			zoom = 16.5;
		} else if (item.areatype == '3') {
			//社区
			address1 = address + properties.SQMC
			zoom = 15;
		} else if (item.areatype == '2') {
			//街道
			zoom = 13;
		} else if (item.areatype == '1') {
			zoom = 11;
		} else if (item.areatype == '1') {
			zoom = 10;
		}
		properties.ZOOM = zoom;
		properties.BZDZ = address1 || ''
		return properties;
	}


	//处理点击得到的数据
	useClickData(res, x, y) {
		let data = res;
		let properties = {};
		Vue.set(properties, 'LON', x);
		Vue.set(properties, 'LAT', y);
		Vue.set(properties, 'SBM', !!data.qu && !!data.qu.parentcode ? data.qu.parentcode : '');
		Vue.set(properties, 'SMC', !!data.qu && !!data.qu.parentname ? data.qu.parentname : '');
		Vue.set(properties, 'QBM', !!data.qu && !!data.qu.areacode ? data.qu.areacode : '');
		Vue.set(properties, 'QMC', !!data.qu && !!data.qu.areaname ? data.qu.areaname : '');
		Vue.set(properties, 'JDBM', !!data.jd && !!data.jd.areacode ? data.jd.areacode : '');
		Vue.set(properties, 'JDMC', !!data.jd && !!data.jd.areaname ? data.jd.areaname : '');
		Vue.set(properties, 'SQBM', !!data.sq && !!data.sq.areacode ? data.sq.areacode : '');
		Vue.set(properties, 'SQMC', !!data.sq && !!data.sq.areaname ? data.sq.areaname : '');
		Vue.set(properties, 'JCWGBM', !!data.wg && !!data.wg.areacode ? data.wg.areacode : '');
		Vue.set(properties, 'JCWGMC', !!data.wg && !!data.wg.areaname ? data.wg.areaname : '');
		Vue.set(properties, 'JZWTYDZBM', !!data.ld && !!data.ld.areacode ? data.ld.areacode : '');
		if (!!data.ld && (!!data.ld.areaname || !!data.ld.ldmc) && (!isBlank(data.ld.areaname) || !isBlank(data.ld.ldmc))) {
			Vue.set(properties, 'LDMC', !isBlank(data.ld.areaname) ? data.ld.areaname : data.ld.ldmc);
		} else {
			Vue.set(properties, 'LDMC', '');
		}
		let properties1 = properties;
		Vue.set(properties, 'BZDZ', properties1.SMC + properties1.QMC + properties1.JDMC + properties1.SQMC + properties1.JCWGMC +
			properties1.LDMC);
		return properties;

	}


	/**
	 * @description: 多面构建FeatureCollection对象
	 * @param {type}
	 * @return:
	 */
	convertFeatureCollection(list, type = true) {
		const FeatureCollection = {
			type: 'FeatureCollection',
			features: []
		};
		// 判断数组
		if (list == undefined || list.length == 0) {
			console.log('请检查传入的list是否为空');
			return FeatureCollection;
		}
		// 2种情况数据处理
		for (const i in list) {
			let areaname;
			if (!type) {
				if (list[i].areaname != ' ') {
					areaname = list[i].areaname;
				} else {
					areaname = '暂无数据';
				}
			} else {
				areaname = list[i]._source.areaname;
			}
			let item = type ? list[i]._source : list[i]
			let properties = {
				LON: item.lon || '',
				LAT: item.lat || '',
				SHENGBM: item.shengdm || "",
				SHENGMC: item.shengmc || "",
				SBM: item.shidm || "",
				SMC: item.shimc || "",
				QBM: item.qudm || "",
				QMC: item.qumc || "",
				JDBM: item.jddm || '',
				JDMC: item.jdmc || '',
				SQBM: item.sqdm || '',
				SQMC: item.sqmc || '',
				JCWGBM: item.wgdm || '',
				JCWGMC: item.wgmc || '',
				JZWTYDZBM: item.lddm || '',
				LDMC: item.ldmc || '',
				areaname: areaname,
				type: i,
				height: Math.round(Math.random() * 100 + 10),
				min_height: 0,
				areacode: item.areacode,
				color: false,
				location: (item.location && item.location.coordinates) ? item.location.coordinates : ''
			}
			//这里字段不全
			let address = properties.SHENGMC + properties.SMC + properties.QMC + properties.JDMC
			let address1;
			if (item.areatype == '6') {
				//楼栋
				address1 = address + properties.SQMC + properties.JCWGMC + properties.LDMC
			} else if (item.areatype == '5') {
				//网格
				address1 = address + properties.SQMC + properties.JCWGMC
			} else if (item.areatype == '4') {
				//社区
				address1 = address + properties.SQMC
			}
			Vue.set(properties, 'BZDZ', address1 || '');

			FeatureCollection.features.push({
				type: 'Feature',
				properties: properties,
				geometry: item.areainfo
			});
		}
		return FeatureCollection;
	}

	//处理道路，小区，标志物数据
	convertRoad(list, name) {
		let FeatureCollection = {
			type: 'FeatureCollection',
			features: []
		};
		if (!!list && list.length > 0) {
			// 2种情况数据处理
			for (const i in list) {
				let areaname
				if (name === 'xqmc') {
					areaname = list[i].xqmc;
				} else if (name === 'bzwmc') {
					areaname = list[i].bzwmc;
				} else {
					areaname = list[i].dlmc;
				}

				let item = list[i].shape
				let properties = {
					areaname: areaname
				}
				FeatureCollection.features.push({
					type: 'Feature',
					properties: properties,
					geometry: item
				});
			}
		}

		return FeatureCollection;
	}

}



export {
	DataManager
}

export default DataManager
